

#include "main.h"

#define HARD_VER_ADR0
#define HARD_VER_ADR1
#define HARD_VER_ADR2
#define HARD_VER_ADR3

#define HSIDE_A_ADC

#define NTC3_MCU_AD
#define NTC4_MCU_AD

#define FAN_FAULT_MCU
#define FAN_D_MCU
#define FAN_A_MCU
#define FAN_IO

#define KL30_AD
#define AD_REF

typedef enum
{
    AD0_24V_IDX=0,
    AD0_T6_IDX,
    AD0_T5_IDX,
    AD0_T4_IDX,
    AD0_T3_IDX,
    AD0_T2_IDX,
    AD0_T1_IDX,
    AD0_IS4_IDX,
    AD0_IS3_IDX,
    AD0_IS2_IDX,
    AD0_IS1_IDX,
    AD0_HALL2_IDX,
    AD0_HALL1_IDX,
    AD0_MAX_IDX
}Ecu_Adc_Cov_Index;


typedef struct 
{
	u32 u32_DataAddr;			/*DMA数据源地址 			eg:结果寄存器ADC1->aR[0]*/
	u32 u32_ResultAddr;		/*DMA存放结果首地址		eg:DMA存放数据的首地址*/
	u8  u8_Count;					/*DMA 中断次数计数*/
}Ecu_Adc_Dma;


const Bsw_Adc_CovFlg Adc0CovFlg_Arr[AD0_MAX_IDX] = 
{
    {ADC0,ADC_INPUTCHAN_EXT1,0},            /*AD0_24V PTA1为ADC0_SE1*/
    {ADC0,ADC_INPUTCHAN_EXT2,0},            /*AD0_T6 PTA6为ADC0_SE2*/
    {ADC0,ADC_INPUTCHAN_EXT6,0},            /*AD0_T5 PTB2为ADC0_SE6*/
    {ADC0,ADC_INPUTCHAN_EXT7,0},            /*AD0_T4 PTB3为ADC0_SE7*/
    {ADC0,ADC_INPUTCHAN_EXT3,0},            /*AD0_T3 PTA7为ADC0_SE3*/
    {ADC0,ADC_INPUTCHAN_EXT12,0},           /*AD0_T2 PTC14为ADC0_SE12*/
    {ADC0,ADC_INPUTCHAN_EXT13,0},           /*AD0_T1 PTC15为ADC0_SE13*/    
    {ADC0,ADC_INPUTCHAN_EXT8,0},            /*AD0_IS4 PTC0为ADC0_SE8*/
    {ADC0,ADC_INPUTCHAN_EXT10,0},           /*AD0_IS3 PTC2为ADC0_SE10*/
    {ADC0,ADC_INPUTCHAN_EXT9,0},            /*AD0_IS2 PTC1为ADC0_SE9*/
    {ADC0,ADC_INPUTCHAN_EXT11,0},           /*AD0_IS1 PTC3为ADC0_SE11*/
    {ADC0,ADC_INPUTCHAN_EXT14,0},           /*AD0_HALL2 PTC16为ADC0_SE14*/
    {ADC0,ADC_INPUTCHAN_EXT15,0},           /*AD0_HALL1 PTC17为ADC0_SE15*/
};

const Bsw_Adc_CovFlg Adc1CovFlg_Arr[6] = 
{
	{ADC1,ADC_INPUTCHAN_EXT8,0},			/*PTB13*/
	{ADC1,ADC_INPUTCHAN_EXT9,0},			/*PTB14*/
	{ADC1,ADC_INPUTCHAN_EXT14,0},			/*PTB15*/
	{ADC1,ADC_INPUTCHAN_EXT15,0},			/*PTB16*/
	
	{ADC1,ADC_INPUTCHAN_EXT14,0x01},	/*PTB0*/
	{ADC1,ADC_INPUTCHAN_EXT15,0x02},	/*PTB1*/
};




BSW_ADC_Cfg   ADC0_Init_Cfg=
{
	.clockName            	=	PCC_ADC0_CLOCK,
	.u32_ClkSoc      		=	6,
	.Adc_base         		=	ADC0,
	.Adc_IRQn				=	ADC0_IRQn,
	.u8_ChIndex         	=	0,	/*此数值不可更改*/
	.u32_instance         	=	0,

	.InputCh            	=	ADC_INPUTCHAN_EXT4,
	.Irq_En					=	false,

	.Sim_baseAddr        	=	SIM,
	.SIM_Ch_Select        	=	0x00,

	.clockDivide          	=	ADC_CLK_DIVIDE_1,
	.resolution           	=	ADC_RESOLUTION_12BIT,
	.inputClock           	=	ADC_CLK_ALT_1,
	.u8_sampletime        	=	150,
	.trigger              	=	ADC_TRIGGER_SOFTWARE,
	.pretriggerSel        	=	ADC_PRETRIGGER_SEL_PDB,
	.b_DMAEnFlag          	=	false,
	.voltageRef           	=	ADC_VOLTAGEREF_VREF,
	.b_ConvFlag        		=	false,
	.b_SupMonEnFlag       	=	true,
	.b_CalActFlag         	=	true,
	.AvrgMdl         		=	ADC_AVERAGE_4,			/*单次转化为4us 均衡4次时间为16us*/
	.b_HwAveEnFlag        	=	true,
	.u16_value_Offset		= 	0,
};


BSW_ADC_Cfg   ADC1_Init_Cfg=
{
	.clockName            	=	PCC_ADC1_CLOCK,
	.u32_ClkSoc      		=	6,
	.Adc_base         		=	ADC1,
	.Adc_IRQn				=	ADC1_IRQn,
	.u8_ChIndex         	=	0,	/*此数值不可更改*/
	.u32_instance         	=	1,

	.InputCh            	=	ADC_INPUTCHAN_EXT8,
	.Irq_En					=	false,

	.Sim_baseAddr        	=	SIM,
	.SIM_Ch_Select        	=	0x00,

	.clockDivide          	=	ADC_CLK_DIVIDE_1,
	.resolution           	=	ADC_RESOLUTION_12BIT,
	.inputClock           	=	ADC_CLK_ALT_1,
	.u8_sampletime        	=	150,
	.trigger              	=	ADC_TRIGGER_SOFTWARE,
	.pretriggerSel        	=	ADC_PRETRIGGER_SEL_PDB,
	.b_DMAEnFlag          	=	false,
	.voltageRef           	=	ADC_VOLTAGEREF_VREF,
	.b_ConvFlag        		=	false,
	.b_SupMonEnFlag       	=	true,
	.b_CalActFlag         	=	true,
	.AvrgMdl         		=	ADC_AVERAGE_4,			/*单次转化为4us 均衡4次时间为16us*/
	.b_HwAveEnFlag        	=	true,
	.u16_value_Offset		= 	0,
};


BSW_Dma_Cfg AD1_Dma =
{
	.DMA_Ch 				=	DMA_CH4,									/*配置DMA通道    									eg: DMA_CH0								*/
	.IRQn					=	DMA4_IRQn,
//	.SrcAddr					=	(u32)TCD0_Source,									/*配置源地址											eg: (u32)Dma_Src					*/
	.SrcOffset				=	0,												/*自动累加源地址大小							eg: 1 每次传输地址自动加1	*/
	.SrcTrfSize 			=	EDMA_TRANSFER_SIZE_2B,		/*源地址单次传输几个字节					eg:	EDMA_TRANSFER_SIZE_1B */
	.SrcLastAdj             = 0,												/*配置传输完成时是否更改源地址		eg: 0 不更改源地址				*/
	
//	.DestAddr					=	(u32)TCD0_Dest,					/*配置目标地址										eg: (u32)Dma_Dest					*/
	.DestOffset				=	2,												/*自动累加目标地址大小						eg: 1 每次传输地址自动加1	*/
	.DestTrfSize			=	EDMA_TRANSFER_SIZE_2B,		/*目标地址单次传输几个字节				eg:	EDMA_TRANSFER_SIZE_1B */
	.DestLastAdj      = 0,												/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址			*/
	
	.SetNbytes				=	2,												/*DMA开启每次传输几个字节					eg: 1  每次传输1个字节		*/
	.SetMjrCount			=	1,												/*DMA开启每次循环的次数 					eg: 1 	每次传输多少次			*/
	.b_MjrCpltIrq			= true,											/*配置是否传输完成中断		*/
	.b_HlfCpltIrq			= false,
	.b_DmaRqstDone		    =	true,											/*传输结束是否自动关闭DMA					eg: true 自动关闭DMA 			*/
	.b_Dma_En			    =	false,										/* DMA使能配置										eg: true 使能DMA触发传输	*/
};


BSW_Dma_Cfg AD0_Dma =
{
	.DMA_Ch 				=	DMA_CH5,									/*配置DMA通道    									eg: DMA_CH0								*/
	.IRQn					=	DMA5_IRQn,
/*	.SrcAddr					=	(u32)TCD0_Source,*/			/*配置源地址											eg: (u32)Dma_Src					*/
	.SrcOffset				=	0,												/*自动累加源地址大小							eg: 1 每次传输地址自动加1	*/
	.SrcTrfSize 			=	EDMA_TRANSFER_SIZE_2B,		/*源地址单次传输几个字节					eg:	EDMA_TRANSFER_SIZE_1B */
	.SrcLastAdj             = 0,												/*配置传输完成时是否更改源地址		eg: 0 不更改源地址				*/
	
/*	.DestAddr					=	(u32)TCD0_Dest,*/				/*配置目标地址										eg: (u32)Dma_Dest					*/
	.DestOffset				=	2,												/*自动累加目标地址大小						eg: 1 每次传输地址自动加1	*/
	.DestTrfSize			=	EDMA_TRANSFER_SIZE_2B,		/*目标地址单次传输几个字节				eg:	EDMA_TRANSFER_SIZE_1B */
	.DestLastAdj            = 0,												/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址			*/
	
	.SetNbytes				=	2,												/*DMA开启每次传输几个字节					eg: 1  每次传输1个字节		*/
	.SetMjrCount			=	1,												/*DMA开启每次循环的次数 					eg: 1 	每次传输多少次			*/
	.b_MjrCpltIrq			= true,											/*配置是否传输完成中断		*/
	.b_HlfCpltIrq			= false,
	.b_DmaRqstDone		    =	true,											/*传输结束是否自动关闭DMA					eg: true 自动关闭DMA 			*/
	.b_Dma_En				=	false,										/* DMA使能配置										eg: true 使能DMA触发传输	*/
};


void ECU_ADC_INIT(void)
{
    /*AD0_T1*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN15_IDX,PORT_PIN_DISABLED);    /*设置端口PTC15为ADC0_SE13模式 */
    /*AD0_T2*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN14_IDX,PORT_PIN_DISABLED);    /*设置端口PTC14为ADC0_SE12模式*/
    /*AD0_T3*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_A,PIN7_IDX,PORT_PIN_DISABLED);     /*设置端口PTA7为ADC0_SE3模式*/
    /*AD0_T4*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_B,PIN3_IDX,PORT_PIN_DISABLED);     /*设置端口PTB3为ADC0_SE7模式*/
    /*AD0_T5*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_B,PIN2_IDX,PORT_PIN_DISABLED);     /*设置端口PTB2为ADC0_SE6模式*/
    /*AD0_T6*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_A,PIN6_IDX,PORT_PIN_DISABLED);     /*设置端口PTA6为ADC0_SE2模式*/

    /*AD0_HALL1*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN17_IDX,PORT_PIN_DISABLED);    /*设置端口PTC17为ADC0_SE15模式*/
    /*AD0_HALL2*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN16_IDX,PORT_PIN_DISABLED);    /*设置端口PTC16为ADC0_SE14模式*/

    /*AD0_24V*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_A,PIN1_IDX,PORT_PIN_DISABLED);     /*设置端口PTA1为ADC0_SE1模式*/
    
    /*AD0_IS1*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN3_IDX,PORT_PIN_DISABLED);     /*设置端口PTC3为ADC0_SE11模式*/
    /*AD0_IS2*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN1_IDX,PORT_PIN_DISABLED);     /*设置端口PTC1为ADC0_SE9模式*/
    /*AD0_IS3*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN2_IDX,PORT_PIN_DISABLED);     /*设置端口PTC2为ADC0_SE10模式*/
    /*AD0_IS4*/
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,PIN0_IDX,PORT_PIN_DISABLED);     /*设置端口PTC0为ADC0_SE8模式*/

    BSW_ADC_Config(ADC0_Init_Cfg);
    /*****************DMA通道对应的设置**********************/
    /*
    BSW_DMAMUX_INIT(AD1_Dma.DMA_Ch,EDMA_REQ_ADC1);
    AD1_Dma.SrcAddr = (u32)&(ADC1->aR[0]);
    AD1_Dma.DestAddr = (u32)&G_u16_Ad1_Rslt_Arr;
    BSW_DMA_INIT_CFG(AD1_Dma);

    BSW_DMAMUX_INIT(AD0_Dma.DMA_Ch,EDMA_REQ_ADC0);
    AD0_Dma.SrcAddr = (u32)&(ADC0->aR[0]);
    AD0_Dma.DestAddr = (u32)&G_u16_Ad0_Rslt_Arr;
    BSW_DMA_INIT_CFG(AD0_Dma);
    */
}

/*
Ecu_Adc_Dma Adc0_Dma =
{
    .u32_DataAddr  	= (u32)&(ADC0->R[0]),
    .u32_ResultAddr = (u32)G_u16_Ad0_Rslt_Arr,
    .u8_Count 			=	0,
};

void ECU_ADC0_StartCov(void)
{
    Adc0_Dma.u8_Count = 0;
    BSW_ADC_StartCov(Adc0CovFlg_Arr[Adc0_Dma.u8_Count]);
}
*/
void ECU_ADC0_IRQ(void)
{
    /*
    G_u16_Ad0_Rslt_Arr[Adc0_Dma.u8_Count]= ADC0->R[0];
    Adc0_Dma.u8_Count ++;
    if(Adc0_Dma.u8_Count < 4)
    {
    	BSW_ADC_StartCov(Adc0CovFlg_Arr[Adc0_Dma.u8_Count]);
    }
    */
}



/******************************ADC1_IRQ********************************/
/*
Ecu_Adc_Dma Adc1_Dma =
{
    .u32_DataAddr  	= (u32)&(ADC1->R[0]),
    .u32_ResultAddr = (u32)G_u16_Ad1_Rslt_Arr,
    .u8_Count 			=	0,
};

void ECU_ADC1_StartCov(void)
{
    Adc1_Dma.u8_Count = 0;
    BSW_ADC_StartCov(Adc1CovFlg_Arr[Adc1_Dma.u8_Count]);
}
*/
void ECU_ADC1_IRQ(void)
{
    /*
    G_u16_Ad1_Rslt_Arr[Adc1_Dma.u8_Count]= ADC1->R[0];
    Adc1_Dma.u8_Count ++;
    if(Adc1_Dma.u8_Count < 4)
    {
    	BSW_ADC_StartCov(Adc1CovFlg_Arr[Adc1_Dma.u8_Count]);
    }
    */
}


/*******************************************************************************/
void ECU_ADC_GET_VALUE(u16* P_u16_AD)
{
	static u8 s_u8_EcuAdc_Step = AD0_24V_IDX;
    
    ADC0_Init_Cfg.InputCh = Adc0CovFlg_Arr[s_u8_EcuAdc_Step].InputCh;
    BSW_ADC_StartConversion(ADC0_Init_Cfg);
    P_u16_AD[s_u8_EcuAdc_Step] = BSW_ADC_GetValue(ADC0_Init_Cfg);
    
    s_u8_EcuAdc_Step+=1;
    if(s_u8_EcuAdc_Step >= AD0_MAX_IDX)
        s_u8_EcuAdc_Step=AD0_24V_IDX;
    
#if 0
    switch(s_u8_EcuAdc_Step)
    {
        case 0:
        {
            ADC1_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT4;			/*KL15_AD*/
            BSW_ADC_StartConversion(ADC1_Init_Cfg);
            P_u16_AD[0] = BSW_ADC_GetValue(ADC1_Init_Cfg); 
            s_u8_EcuAdc_Step = 1;
            break;
        }
        case 1:
        {
            ADC1_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT5;			/*NTCH2_AD0*/
            BSW_ADC_StartConversion(ADC1_Init_Cfg);
            P_u16_AD[1] = BSW_ADC_GetValue(ADC1_Init_Cfg);	
            s_u8_EcuAdc_Step = 2;
            break;
        }
        case 2:
        {
            ADC1_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT10;		/*HSIDE_A_AD*/
            BSW_ADC_StartConversion(ADC1_Init_Cfg);
            P_u16_AD[2] = BSW_ADC_GetValue(ADC1_Init_Cfg);	
            s_u8_EcuAdc_Step = 3;
            break;
        }
        case 3:
        {
            ADC1_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT11;		/*NTC2_AD1*/
            BSW_ADC_StartConversion(ADC1_Init_Cfg);
            P_u16_AD[3] = BSW_ADC_GetValue(ADC1_Init_Cfg);	
            s_u8_EcuAdc_Step = 4;
            break;
        }
        /*************************ADC0*****************************/
        case 4:
        {
            ADC0_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT1;		/*ADC_REF*/
            BSW_ADC_StartConversion(ADC0_Init_Cfg);
            P_u16_AD[4] = BSW_ADC_GetValue(ADC0_Init_Cfg);	
            s_u8_EcuAdc_Step = 5;
            break;
        }
        case 5:
        {
            ADC0_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT3;		/*RELAY+*/
            BSW_ADC_StartConversion(ADC0_Init_Cfg);
            P_u16_AD[5] = BSW_ADC_GetValue(ADC0_Init_Cfg);	
            s_u8_EcuAdc_Step = 6;
            break;
        }
        case 6:
        {
            ADC0_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT4;		/*NTCH4_MCU*/
            BSW_ADC_StartConversion(ADC0_Init_Cfg);
            P_u16_AD[6] = BSW_ADC_GetValue(ADC0_Init_Cfg);	
            s_u8_EcuAdc_Step = 7;
            break;
        }
        case 7:
        {
            ADC0_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT5;		/*SBC_MUX_OUT*/
            BSW_ADC_StartConversion(ADC0_Init_Cfg);
            P_u16_AD[7] = BSW_ADC_GetValue(ADC0_Init_Cfg);	
            s_u8_EcuAdc_Step = 8;
            break;
        }
        case 8:
        {
            ADC0_Init_Cfg.InputCh =	ADC_INPUTCHAN_EXT8;		/*NTC1_AD1*/
            BSW_ADC_StartConversion(ADC0_Init_Cfg);
            P_u16_AD[8] = BSW_ADC_GetValue(ADC0_Init_Cfg);	
            s_u8_EcuAdc_Step = 0;
            break;
        }
        default:
        {
            s_u8_EcuAdc_Step = 0;
            break;
        }
    }
#endif
}


/********DMA5_IRQ********/
void ECU_ADC0_CovDma(void)
{
}

/********DMA4_IRQ********/
void ECU_ADC1_CovDma(void)
{
}

/*********************APP-ADC*************************/
u16 G_u16_Ad0_Rslt_Arr[AD0_MAX_IDX]={0};
u16 G_u16_Ad1_Rslt_Arr[12] = {0};

void Ecu_ADC0_Main(void)
{
    ECU_ADC_GET_VALUE(G_u16_Ad0_Rslt_Arr);

#if 0
    f_HSIDE_A_Val = G_u16_BMS_AD_Arr[2];
    f_HSIDE_A_Val = f_HSIDE_A_Val * 12/4095;

    f_KL30_Val = G_u16_BMS_AD_Arr[7];
    BSW_12V_SupplyPowerVoltage = f_KL30_Val * 22.25/4095;/*大约为采集值的6,9倍 稍微小点  6.9*3.3=22.77 修正一下 22.25*/

    f_AD_REF_Val = G_u16_BMS_AD_Arr[1];
    f_AD_REF_Val = f_AD_REF_Val * 6.6/4095;

    f_RELAY_Val = G_u16_BMS_AD_Arr[5];
    f_RELAY_Val = f_RELAY_Val * 0.0088645 - 0.257;

    App_Bms_StsFlg.BSW_AD_Flg = true;
#endif
}


